package com.yang.modules.common.deploy.amqp.rabbitmq.consumer;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import org.yang.framework.businesstool.amqp.rabbit.constants.FanoutConstants;
import org.yang.framework.businesstool.amqp.rabbit.constants.HeaderConstants;
import org.yang.framework.businesstool.amqp.rabbit.util.ConsumerUtil;
import org.yang.framework.deploy.amqp.strategy.StrategyConstants;

/**
 * @author Billyang
 * @date 2024/9/18 14:24
 */
@Component
public class RefreshConsumer {

    /**
     * <pre>
     * 临时队列名由fanout.deploy-${spring.application.name}-${spring.cloud.client.ip-address}-${server.port}构成
     * 每个服务（包括同个服务的集群部署）都要有单独的队列来监听fanout exchange的消息（用于监听其他服务下线或上线事件，重新拉取注册信息）
     * </pre>
     * exclusive表示独占队列：仅创建该队列的连接能够访问队列。
     * 连接关闭时自动删除队列：当创建此队列的连接关闭时，队列会被自动删除。
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = FanoutConstants.DEPLOY.EXCHANGE+ "-${spring.application.name}-${spring.cloud.client.ip-address}-${server.port}", durable = "false", autoDelete = "true", exclusive = "true"),
            exchange = @Exchange(value = FanoutConstants.DEPLOY.EXCHANGE, type = ExchangeTypes.FANOUT, durable = "false",autoDelete = "true")
    ))
    public void refresh(String jsonStrMsg, Channel channel, Message message) {
        message.getMessageProperties().setHeader(HeaderConstants.HEADER_STRATEGY_NAME, StrategyConstants.REFRESH_STRATEGY);
        ConsumerUtil.handleByStrategy(jsonStrMsg, channel, message);
    }
}
